//
//  RecommendUserController.m
//  collection
//
//  Created by 张靖 on 2/5/12.
//  Copyright (c) 2012 Tsinghua University. All rights reserved.
//

#import "RecommendUserController.h"
#import "UserProfileController.h"
#import "UserInfo.h"
#import "UserRelationCell.h"
#import "CollectionDataManager.h"
#import "MBProgressHUDHelper.h"


@interface RecommendUserController()

@property (retain, nonatomic) IBOutlet UIPullingheaderTableView *recommendTableView;

- (IBAction)onClickBackBarButton:(id)sender;
- (IBAction)onClickLoadMore:(id)sender;

@property (nonatomic, strong)   NSMutableArray* recommendUserList;

@end

@implementation RecommendUserController
@synthesize recommendTableView = _recommendTableView;

@synthesize recommendUserList   = _recommendUserList;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.hidesBottomBarWhenPushed = YES;
        
        self.recommendUserList = [[NSMutableArray alloc] init];
        
        CollectionDataManager* sharedDataManager = [CollectionDataManager sharedInstance];
        for (int i = 0; i <= 8; i++) {
            UserInfo* user = [sharedDataManager cacheUserByUserID:(i + 1)];
            
            [self.recommendUserList addObject:user];
        }
    }
    return self;
}

- (void)dealloc {
    
    self.recommendUserList  = nil;
    
    [_recommendTableView release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [self setRecommendTableView:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Implementation of UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return [self.recommendUserList count];
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString* followeeCellID = @"UserRelationCellID";
    
    UserRelationCell* cell = (UserRelationCell*)[tableView dequeueReusableCellWithIdentifier:followeeCellID];
    if (cell == nil) {
        NSArray* views = [[NSBundle mainBundle] loadNibNamed:@"UserRelationCell" owner:self options:nil];
        cell = [views objectAtIndex:0];
    }
    
    UserInfo* user = [self.recommendUserList objectAtIndex:indexPath.row];
    cell.avatarImageView.image = user.avatar;
    cell.nicknameLabel.text = user.nickname;
    cell.signatureLabel.text = user.signature;
    cell.actionButton.hidden = YES;
    
    return cell;
}

#pragma mark - [UITableViewDelegate]

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell* cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    
    return cell.frame.size.height;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UserInfo* user = [self.recommendUserList objectAtIndex:indexPath.row];
    
    UserProfileController* userProfileController = [[UserProfileController alloc] initWithNibName:@"UserProfileController" bundle:nil];
    userProfileController.userID = user.userID;
    [self.navigationController pushViewController:userProfileController animated:YES];
    [userProfileController release];
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark - [UIPullingheaderDelegate]

- (void)onInvokePullingRefresh:(UIPullingheaderTableView *)view {
    
    [self refresh];
}

- (void)delayFinishedRefreshLoading {
    
    [self.recommendTableView reloadData];
    [self.recommendTableView finishedRefreshLoading:0.0f];
    
    MBProgressHUD* indicatorView = [MBProgressHUDHelper popUpMBProcessHUD:self.view delegate:nil];
    [MBProgressHUDHelper changeToMBSuccessMode:indicatorView customMsg:@"更新完毕" afterDelay:1.0f];
}

- (void)refresh {
    
    NSLog(@"Refresh");
    [NSTimer scheduledTimerWithTimeInterval:2.0f
                                     target:self 
                                   selector:@selector(delayFinishedRefreshLoading)
                                   userInfo:nil
                                    repeats:NO];
}

#pragma mark - Private methods

- (IBAction)onClickBackBarButton:(id)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)onClickLoadMore:(id)sender {
    
}

@end
